package top.fangw.common.log.aspect;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import top.fangw.common.log.annotation.Log;

import java.lang.reflect.Method;

/**
 * @description: 日志手机切面类
 * @author: fgw
 * @createDate: 2024/1/17
 */

@Component
@Aspect
@Slf4j
@RequiredArgsConstructor
public class LogAspect {
    @Pointcut("@annotation(top.fangw.common.log.annotation.Log)")
    public void log() {
    }

    @Around("log()")
    public Object logAround(ProceedingJoinPoint proceedingJoinPoint) {
        Object result = null;
        try {
            // 获取被代理的对象
            Object target = proceedingJoinPoint.getTarget();
            result = proceedingJoinPoint.proceed();
            // 获取通知签名
            MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
            Method method = target.getClass().getMethod(signature.getName(), signature.getParameterTypes());
            // 获取方法上的注解
            Log logA = method.getAnnotation(Log.class);
            log.info("result信息：{}", result);
            log.info("log信息：{}", logA.appName());
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
        return result;
    }
}
